#! /usr/bin/env python3
# -*- coding: utf-8 -*-

import logging


def __log():
    # create logger with 'spam_application'
    logger = logging.getLogger('server')
    logger.setLevel(logging.DEBUG)
    # create file handler which logs even debug messages
    fh = logging.FileHandler('./log/server.log')
    fh.setLevel(logging.DEBUG)
    # create console handler with a higher log level
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    # create formatter and add it to the handlers
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)
    # add the handlers to the logger
    logger.addHandler(fh)
    logger.addHandler(ch)

    return logger


_log = __log()


def log(*args, **kwargs):
    logger = {
        'debug': _log.debug,
        'info': _log.info,
        'error': _log.error,
    }
    if 'level' in kwargs:
        lg = logger[kwargs['level']]
    else:
        lg = logger['info']
    message = ''.join([str(i) for i in args])
    lg(message)


encode_code = 'utf-8'
